package com.itttq.gen.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itttq.gen.model.po.ProjectGenHisPo;
import com.itttq.gen.model.po.ProjectPo;
import com.itttq.gen.model.qo.ProjectHisQo;
import com.itttq.gen.model.to.ResultTo;
import com.itttq.gen.mapper.IProjectHisMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
public class ProjectGenHisService {
    @Autowired
    private IProjectHisMapper projectHisMapper;

    /**
     * 根据ID查询对象信息
     *
     * @param id
     */
    public ProjectGenHisPo getProjectHis(long id) {
        return projectHisMapper.selectById(id);
    }

    /**
     * 根据ID删除对象
     *
     * @param id
     */
    public void delProjectHis(long id) {
        int rs = projectHisMapper.deleteById(id);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "删除数据失败"));
    }

    /**
     * 根据ID集合批量删除对象
     *
     * @param ids ID的List集合
     */
    public void delProjectHisByIds(List<Long> ids) {
        int rs = projectHisMapper.deleteBatchIds(ids);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "批量删除数据失败"));
    }

    /**
     * 新增对象
     *
     * @param projectHis 新增的PO对象
     */
    public void addProjectHis(ProjectGenHisPo projectHis) {
        int rs = projectHisMapper.insert(projectHis);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "新增数据失败"));
    }

    /**
     * 根据ID更新对象，对象的ID值必须传入
     *
     * @param projectHis 更新的PO对象，必传ID值
     */
    public void updateProjectHis(ProjectGenHisPo projectHis) {
        int rs = projectHisMapper.updateById(projectHis);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "更新数据失败"));
    }

    /**
     * 根据条件查询记录数
     *
     * @param projectHisQo 查询对象
     * @return int 记录总数
     */
    public int getProjectHisCount(ProjectHisQo projectHisQo) {
        return projectHisMapper.selectCount(getQw(projectHisQo));
    }

    /**
     * 获取所有数据列表，不分页
     *
     * @param projectHisQo 查询对象
     * @return List<ProjectHisPo>
     */
    public List<ProjectGenHisPo> getProjectHiss(ProjectHisQo projectHisQo) {
        return projectHisMapper.selectList(getQw(projectHisQo));
    }

    /**
     * 获取所有数据列表，并分页
     *
     * @param projectHisQo 查询对象
     * @return IPage<ProjectHisPo>
     */
    public IPage<ProjectGenHisPo> getProjectHisAndPage(ProjectHisQo projectHisQo) {
        return projectHisMapper.selectPage(new Page<>(projectHisQo.getPageNo(), projectHisQo.getPageSize()), getQw(projectHisQo));
    }

    /**
     * 构建Mybatis-Plus的查询条件对象。
     *
     * @param projectHisQo 查询对象
     * @return LambdaQueryWrapper<ProjectHisPo>
     */
    private LambdaQueryWrapper<ProjectGenHisPo> getQw(ProjectHisQo projectHisQo) {
        return new QueryWrapper<ProjectGenHisPo>().lambda()
                .eq(null != projectHisQo.getProjectId(), ProjectGenHisPo::getProjectId, projectHisQo.getProjectId())
                .eq(null != projectHisQo.getResult(), ProjectGenHisPo::getResult, projectHisQo.getResult())
                .orderByDesc(ProjectGenHisPo::getCreateTime);
    }
}
